home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-3.98 / bfd.mips / targets.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-23  |  14.6 KB  |  480 lines

  1. /* Generic target-file-type support for the BFD library.
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* $Id: targets.c,v 1.23 1991/07/31 16:58:03 gnu Exp $ */
  22.  
  23. #include <sysdep.h>
  24. #include "bfd.h"
  25. #include "libbfd.h"
  26.  
  27. /*doc*
  28. @section Targets
  29. Each port of BFD to a different machine requries the creation of a
  30. target back end. All the back end provides to the root part of bfd is
  31. a structure containing pointers to functions which perform certain low
  32. level operations on files. BFD translates the applications's requests
  33. through a pointer into calls to the back end routines.
  34.  
  35. When a file is opened with @code{bfd_openr}, its format and target are
  36. unknown. BFD uses various mechanisms to determine how to interpret the
  37. file. The operatios performed are:
  38. @itemize @bullet
  39. @item
  40. First a bfd is created by calling the internal routine
  41. @code{new_bfd}, then @code{bfd_find_target} is called with the target
  42. string supplied to @code{bfd_openr} and the new bfd pointer. 
  43. @item
  44. If a null target string was provided to
  45. @code{bfd_find_target}, it looks up the environment variable
  46. @code{GNUTARGET} and uses that as the target string. 
  47. @item
  48. If the target string is still NULL, or the target string
  49. is @code{default}, then the first item in the target vector is used as
  50. the target type. @xref{targets}.
  51. @item
  52. Otherwise, the elements in the target vector are
  53. inspected one by one, until a match on target name is found. When
  54. found, that is used.
  55. @item 
  56. Otherwise the error @code{invalid_target} is returned to
  57. @code{bfd_openr}.
  58. @item 
  59. @code{bfd_openr} attempts to open the file using
  60. @code{bfd_open_file}, and returns the bfd.
  61. @end itemize
  62. Once the bfd has been opened and the target selected, the file format
  63. may be determined. This is done by calling @code{bfd_check_format} on
  64. the bfd with a suggested format. The routine returns @code{true} when
  65. the application guesses right.
  66. */
  67.  
  68.  
  69. /*proto* bfd_target
  70. @node bfd_target
  71. @subsection bfd_target
  72. This structure contains everything that BFD knows about a target.
  73. It includes things like its byte order, name, what routines to call
  74. to do various operations, etc.   
  75.  
  76. Every BFD points to a target structure with its "xvec" member. 
  77.  
  78.  
  79. Shortcut for declaring fields which are prototyped function pointers,
  80. while avoiding anguish on compilers that don't support protos.
  81.  
  82. $#define SDEF(ret, name, arglist) \
  83. $                PROTO(ret,(*name),arglist)
  84. $#define SDEF_FMT(ret, name, arglist) \
  85. $                PROTO(ret,(*name[bfd_type_end]),arglist)
  86.  
  87. These macros are used to dispatch to functions through the bfd_target
  88. vector. They are used in a number of macros further down in bfd.h, and
  89. are also used when calling various routines by hand inside the bfd
  90. implementation.  The "arglist" argument must be parenthesized; it
  91. contains all the arguments to the called function.
  92.  
  93. $#define BFD_SEND(bfd, message, arglist) \
  94. $               ((*((bfd)->xvec->message)) arglist)
  95.  
  96. For operations which index on the bfd format 
  97.  
  98. $#define BFD_SEND_FMT(bfd, message, arglist) \
  99. $            (((bfd)->xvec->message[(int)((bfd)->format)]) arglist)
  100.  
  101. This is the struct which defines the type of BFD this is.  The
  102. "xvec" member of the struct bfd itself points here.  Each module
  103. that implements access to a different target under BFD, defines
  104. one of these.
  105.  
  106. FIXME, these names should be rationalised with the names of the
  107. entry points which call them. Too bad we can't have one macro to
  108. define them both! 
  109.  
  110. *+++
  111.  
  112. $typedef struct bfd_target
  113. ${
  114.  
  115. identifies the kind of target, eg SunOS4, Ultrix, etc 
  116.  
  117. $  char *name;
  118.  
  119. The "flavour" of a back end is a general indication about the contents
  120. of a file.
  121.  
  122. $  enum target_flavour_enum {
  123. $    bfd_target_aout_flavour_enum,
  124. $    bfd_target_coff_flavour_enum,
  125. $    bfd_target_ieee_flavour_enum,
  126. $    bfd_target_oasys_flavour_enum,
  127. $    bfd_target_srec_flavour_enum} flavour;
  128.  
  129. The order of bytes within the data area of a file.
  130.  
  131. $  boolean byteorder_big_p;
  132.  
  133. The order of bytes within the header parts of a file.
  134.  
  135. $  boolean header_byteorder_big_p;
  136.  
  137. This is a mask of all the flags which an executable may have set -
  138. from the set @code{NO_FLAGS}, @code{HAS_RELOC}, ...@code{D_PAGED}.
  139.  
  140. $  flagword object_flags;       
  141.  
  142. This is a mask of all the flags which a section may have set - from
  143. the set @code{SEC_NO_FLAGS}, @code{SEC_ALLOC}, ...@code{SET_NEVER_LOAD}.
  144.  
  145. $  flagword section_flags;
  146.  
  147. The pad character for filenames within an archive header.
  148.  
  149. $  char ar_pad_char;            
  150.  
  151. The maximum number of characters in an archive header.
  152.  
  153. $ unsigned short ar_max_namelen;
  154.  
  155. The minimum alignment restriction for any section.
  156.  
  157. $  unsigned int align_power_min;
  158.  
  159. Entries for byte swapping for data. These are different to the other
  160. entry points, since they don't take bfd as first arg.  Certain other handlers
  161. could do the same.
  162.  
  163. $  SDEF (bfd_vma,      bfd_getx64, (bfd_byte *));
  164. $  SDEF (void,         bfd_putx64, (bfd_vma, bfd_byte *));
  165. $  SDEF (bfd_vma, bfd_getx32, (bfd_byte *));
  166. $  SDEF (void,         bfd_putx32, (bfd_vma, bfd_byte *));
  167. $  SDEF (bfd_vma, bfd_getx16, (bfd_byte *));
  168. $  SDEF (void,         bfd_putx16, (bfd_vma, bfd_byte *));
  169.  
  170. Byte swapping for the headers
  171.  
  172. $  SDEF (bfd_vma,   bfd_h_getx64, (bfd_byte *));
  173. $  SDEF (void,          bfd_h_putx64, (bfd_vma, bfd_byte *));
  174. $  SDEF (bfd_vma,  bfd_h_getx32, (bfd_byte *));
  175. $  SDEF (void,          bfd_h_putx32, (bfd_vma, bfd_byte *));
  176. $  SDEF (bfd_vma,  bfd_h_getx16, (bfd_byte *));
  177. $  SDEF (void,          bfd_h_putx16, (bfd_vma, bfd_byte *));
  178.  
  179. Format dependent routines, these turn into vectors of entry points
  180. within the target vector structure; one for each format to check.
  181.  
  182. Check the format of a file being read.  Return bfd_target * or zero. 
  183.  
  184. $  SDEF_FMT (struct bfd_target *, _bfd_check_format, (bfd *));
  185.  
  186. Set the format of a file being written.  
  187.  
  188. $  SDEF_FMT (boolean,            _bfd_set_format, (bfd *));
  189.  
  190. Write cached information into a file being written, at bfd_close. 
  191.  
  192. $  SDEF_FMT (boolean,            _bfd_write_contents, (bfd *));
  193.  
  194. The following functions are defined in @code{JUMP_TABLE}. The idea is
  195. that the back end writer of @code{foo} names all the routines
  196. @code{foo_}@var{entry_point}, @code{JUMP_TABLE} will built the entries
  197. in this structure in the right order.
  198.  
  199. Core file entry points
  200.  
  201. $  SDEF (char *, _core_file_failing_command, (bfd *));
  202. $  SDEF (int,    _core_file_failing_signal, (bfd *));
  203. $  SDEF (boolean, _core_file_matches_executable_p, (bfd *, bfd *));
  204.  
  205. Archive entry points
  206.  
  207. $ SDEF (boolean, _bfd_slurp_armap, (bfd *));
  208. $ SDEF (boolean, _bfd_slurp_extended_name_table, (bfd *));
  209. $ SDEF (void,   _bfd_truncate_arname, (bfd *, CONST char *, char *));
  210. $ SDEF (boolean, write_armap, (bfd *arch, 
  211. $                              unsigned int elength,
  212. $                              struct orl *map,
  213. $                              int orl_count, 
  214. $                              int stridx));
  215.  
  216. Standard stuff.
  217.  
  218. $  SDEF (boolean, _close_and_cleanup, (bfd *));
  219. $  SDEF (boolean, _bfd_set_section_contents, (bfd *, sec_ptr, PTR,
  220. $                                            file_ptr, bfd_size_type));
  221. $  SDEF (boolean, _bfd_get_section_contents, (bfd *, sec_ptr, PTR, 
  222. $                                            file_ptr, bfd_size_type));
  223. $  SDEF (boolean, _new_section_hook, (bfd *, sec_ptr));
  224.  
  225. Symbols and reloctions
  226.  
  227. $ SDEF (unsigned int, _get_symtab_upper_bound, (bfd *));
  228. $  SDEF (unsigned int, _bfd_canonicalize_symtab,
  229. $           (bfd *, struct symbol_cache_entry **));
  230. $  SDEF (unsigned int, _get_reloc_upper_bound, (bfd *, sec_ptr));
  231. $  SDEF (unsigned int, _bfd_canonicalize_reloc, (bfd *, sec_ptr, arelent **,
  232. $                                               struct symbol_cache_entry**));
  233. $  SDEF (struct symbol_cache_entry  *, _bfd_make_empty_symbol, (bfd *));
  234. $  SDEF (void,     _bfd_print_symbol, (bfd *, PTR, struct symbol_cache_entry  *,
  235. $                                      bfd_print_symbol_enum_type));
  236. $#define bfd_print_symbol(b,p,s,e) BFD_SEND(b, _bfd_print_symbol, (b,p,s,e))
  237. $  SDEF (alent *,   _get_lineno, (bfd *, struct symbol_cache_entry  *));
  238. $
  239. $  SDEF (boolean,   _bfd_set_arch_mach, (bfd *, enum bfd_architecture,
  240. $                                       unsigned long));
  241. $
  242. $  SDEF (bfd *,  openr_next_archived_file, (bfd *arch, bfd *prev));
  243. $  SDEF (boolean, _bfd_find_nearest_line,
  244. $        (bfd *abfd, struct sec  *section,
  245. $         struct symbol_cache_entry  **symbols,bfd_vma offset,
  246. $        CONST char **file, CONST char **func, unsigned int *line));
  247. $  SDEF (int,    _bfd_stat_arch_elt, (bfd *, struct stat *));
  248. $
  249. $  SDEF (int,    _bfd_sizeof_headers, (bfd *, boolean));
  250. $
  251. $  SDEF (void, _bfd_debug_info_start, (bfd *));
  252. $  SDEF (void, _bfd_debug_info_end, (bfd *));
  253. $  SDEF (void, _bfd_debug_info_accumulate, (bfd *, struct sec  *));
  254.  
  255. Special entry points for gdb to swap in coff symbol table parts
  256.  
  257. $  SDEF(void, _bfd_coff_swap_aux_in,(
  258. $       bfd            *abfd ,
  259. $       PTR             ext,
  260. $       int             type,
  261. $       int             class ,
  262. $       PTR             in));
  263. $
  264. $  SDEF(void, _bfd_coff_swap_sym_in,(
  265. $       bfd            *abfd ,
  266. $       PTR             ext,
  267. $       PTR             in));
  268. $
  269. $  SDEF(void, _bfd_coff_swap_lineno_in,  (
  270. $       bfd            *abfd,
  271. $       PTR            ext,
  272. $       PTR             in));
  273. $
  274. $} bfd_target;
  275.  
  276. *---
  277.  
  278. */
  279. extern bfd_target ecoff_little_vec;
  280. extern bfd_target ecoff_big_vec;
  281. extern bfd_target sunos_big_vec;
  282. extern bfd_target demo_64_vec;
  283. extern bfd_target srec_vec;
  284. extern bfd_target b_out_vec_little_host;
  285. extern bfd_target b_out_vec_big_host;
  286. extern bfd_target icoff_little_vec;
  287. extern bfd_target icoff_big_vec;
  288. extern bfd_target ieee_vec;
  289. extern bfd_target oasys_vec;
  290. extern bfd_target m88k_bcs_vec;
  291. extern bfd_target m68kcoff_vec;
  292. extern bfd_target i386coff_vec;
  293. #ifdef DEFAULT_VECTOR
  294. extern bfd_target DEFAULT_VECTOR;
  295. #endif
  296.  
  297. #ifdef GNU960
  298. #define ICOFF_LITTLE_VEC        icoff_little_vec
  299. #define ICOFF_BIG_VEC           icoff_big_vec
  300. #define B_OUT_VEC_LITTLE_HOST   b_out_vec_little_host
  301. #define B_OUT_VEC_BIG_HOST      b_out_vec_big_host
  302. #endif /* GNU960 */
  303.  
  304. #ifndef RESTRICTED
  305. #define ECOFF_LITTLE_VEC        ecoff_little_vec
  306. #define ECOFF_BIG_VEC           ecoff_big_vec
  307. #define ICOFF_LITTLE_VEC        icoff_little_vec
  308. #define ICOFF_BIG_VEC           icoff_big_vec
  309. #define ZB_OUT_VEC_LITTLE_HOST  b_out_vec_little_host
  310. #define ZB_OUT_VEC_BIG_HOST     b_out_vec_big_host
  311. #define SUNOS_VEC_BIG_HOST      sunos_big_vec
  312. #define DEMO_64_VEC             demo_64_vec
  313. #define OASYS_VEC               oasys_vec
  314. #define IEEE_VEC                ieee_vec
  315. #define M88K_BCS_VEC            m88k_bcs_vec
  316. #define SREC_VEC                srec_vec
  317. #define M68KCOFF_VEC            m68kcoff_vec
  318. #define I386COFF_VEC            i386coff_vec
  319. #endif
  320.  
  321. bfd_target *target_vector[] = {
  322.  
  323. #ifdef DEFAULT_VECTOR
  324.         &DEFAULT_VECTOR,
  325. #endif /* DEFAULT_VECTOR */
  326.  
  327. #ifdef  I386COFF_VEC
  328.         &I386COFF_VEC,
  329. #endif  /* I386COFF_VEC */
  330.  
  331. #ifdef ECOFF_LITTLE_VEC
  332.         &ECOFF_LITTLE_VEC,
  333. #endif
  334.  
  335. #ifdef ECOFF_BIG_VEC
  336.         &ECOFF_BIG_VEC,
  337. #endif
  338. #ifdef IEEE_VEC
  339.         &IEEE_VEC,
  340. #endif /* IEEE_VEC */
  341.  
  342. #ifdef OASYS_VEC
  343.         &OASYS_VEC,
  344. #endif /* OASYS_VEC */
  345.  
  346. #ifdef SUNOS_VEC_BIG_HOST
  347.         &SUNOS_VEC_BIG_HOST,
  348. #endif /* SUNOS_BIG_VEC */
  349.  
  350.  
  351. #ifdef HOST_64_BIT
  352. #ifdef DEMO_64_VEC
  353.         &DEMO_64_VEC,
  354. #endif
  355. #endif
  356.  
  357. #ifdef M88K_BCS_VEC
  358.         &M88K_BCS_VEC,
  359. #endif /* M88K_BCS_VEC */
  360.  
  361. #ifdef SREC_VEC
  362.         &SREC_VEC,
  363. #endif /* SREC_VEC */
  364.         
  365. #ifdef ICOFF_LITTLE_VEC
  366.         &ICOFF_LITTLE_VEC,
  367. #endif /* ICOFF_LITTLE_VEC */
  368.  
  369. #ifdef ICOFF_BIG_VEC
  370.         &ICOFF_BIG_VEC,
  371. #endif /* ICOFF_BIG_VEC */
  372.  
  373. #ifdef B_OUT_VEC_LITTLE_HOST
  374.         &B_OUT_VEC_LITTLE_HOST,
  375. #endif /* B_OUT_VEC_LITTLE_HOST */
  376.  
  377. #ifdef B_OUT_VEC_BIG_HOST
  378.         &B_OUT_VEC_BIG_HOST,
  379. #endif /* B_OUT_VEC_BIG_HOST */
  380.  
  381. #ifdef  M68KCOFF_VEC
  382.         &M68KCOFF_VEC,
  383. #endif  /* M68KCOFF_VEC */
  384.  
  385. #ifdef    A29KCOFF_BIG_VEC
  386.     &A29KCOFF_BIG_VEC,
  387. #endif    /* A29KCOFF_BIG_VEC */
  388.  
  389.         NULL, /* end of list marker */
  390. };
  391.  
  392.  
  393. /* default_vector[0] contains either the address of the default vector,
  394.    if there is one, or zero if there isn't.  */
  395.  
  396. bfd_target *default_vector[] = {
  397. #ifdef DEFAULT_VECTOR
  398.         &DEFAULT_VECTOR,
  399. #endif
  400.         0,
  401. };
  402.  
  403.  
  404.  
  405.  
  406. /*proto*
  407. *i bfd_find_target
  408. Returns a pointer to the transfer vector for the object target
  409. named target_name.  If target_name is NULL, chooses the one in the
  410. environment variable GNUTARGET; if that is null or not defined then
  411. the first entry in the target list is chosen.  Passing in the
  412. string "default" or setting the environment variable to "default"
  413. will cause the first entry in the target list to be returned,
  414. and "target_defaulted" will be set in the bfd.  This causes
  415. bfd_check_format to loop over all the targets to find the one
  416. that matches the file being read.  
  417. *; PROTO(bfd_target *, bfd_find_target,(CONST char *, bfd *));
  418. *-*/
  419.  
  420. bfd_target *
  421. DEFUN(bfd_find_target,(target_name, abfd),
  422.       CONST char *target_name AND
  423.       bfd *abfd)
  424. {
  425.   bfd_target **target;
  426.   extern char *getenv ();
  427.   CONST char *targname = (target_name ? target_name : getenv ("GNUTARGET"));
  428.  
  429.   /* This is safe; the vector cannot be null */
  430.   if (targname == NULL || !strcmp (targname, "default")) {
  431.     abfd->target_defaulted = true;
  432.     return abfd->xvec = target_vector[0];
  433.   }
  434.  
  435.   abfd->target_defaulted = false;
  436.  
  437.   for (target = &target_vector[0]; *target != NULL; target++) {
  438.     if (!strcmp (targname, (*target)->name))
  439.       return abfd->xvec = *target;
  440.   }
  441.  
  442.   bfd_error = invalid_target;
  443.   return NULL;
  444. }
  445.  
  446.  
  447. /*proto*
  448. *i bfd_target_list
  449. This function returns a freshly malloced NULL-terminated vector of the
  450. names of all the valid bfd targets. Do not modify the names 
  451. *; PROTO(CONST char **,bfd_target_list,());
  452.  
  453. *-*/
  454.  
  455. CONST char **
  456. DEFUN_VOID(bfd_target_list)
  457. {
  458.   int vec_length= 0;
  459.   bfd_target **target;
  460. CONST  char **name_list, **name_ptr;
  461.  
  462.   for (target = &target_vector[0]; *target != NULL; target++)
  463.     vec_length++;
  464.  
  465.   name_ptr = 
  466.     name_list = (CONST char **) zalloc ((vec_length + 1) * sizeof (char **));
  467.  
  468.   if (name_list == NULL) {
  469.     bfd_error = no_memory;
  470.     return NULL;
  471.   }
  472.  
  473.  
  474.  
  475.   for (target = &target_vector[0]; *target != NULL; target++)
  476.     *(name_ptr++) = (*target)->name;
  477.  
  478.   return name_list;
  479. }
  480.